#include "uart_logger.h"

#include <stdio.h>

#ifndef USB_PRINT
int fputc(int ch, FILE *stream)
{
    while (!(USART1->SR & USART_SR_TXE)) { }
    USART1->DR = (uint8_t)ch;
    
    return ch;
}
#endif

/* PA09 Output Log */
void uart_logger_init()
{
    __HAL_RCC_USART1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();

    UART_HandleTypeDef huart = {0};
    huart.Instance = USART1;
    UART_InitTypeDef *uart = &huart.Init;
    uart->BaudRate = 115200;
    uart->Mode = UART_MODE_TX;
    uart->OverSampling = UART_OVERSAMPLING_16;
    uart->Parity = UART_PARITY_NONE;
    uart->StopBits = UART_STOPBITS_1;
    uart->WordLength = UART_WORDLENGTH_8B;
    uart->HwFlowCtl = UART_HWCONTROL_NONE;
    HAL_UART_Init(&huart);

    GPIO_InitTypeDef gpio = {0};
    gpio.Mode = GPIO_MODE_AF_PP;
    gpio.Pin = GPIO_PIN_9;
    gpio.Speed = GPIO_SPEED_HIGH;

    HAL_GPIO_Init(GPIOA, &gpio);
}
